package one.stand.service.impl;

import lombok.extern.slf4j.Slf4j;
import one.stand.advice.ResultEnum;
import one.stand.mapper.GoodSpecMapperExt;
import one.stand.mapper.ShopCartMapperExt;
import one.stand.model.GoodSpec;
import one.stand.model.Goods;
import one.stand.model.ShopCart;
import one.stand.service.GoodsBaseService;
import one.stand.service.ShopCartService;
import one.stand.util.AssertUtil;
import one.stand.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ShopCartServiceImpl implements ShopCartService {

    @Autowired
    private GoodsBaseService goodsBaseService;
    @Autowired
    private GoodSpecMapperExt goodSpecMapperExt;

    @Autowired
    private ShopCartMapperExt shopCartMapperExt;

    @Override
    public void save(Integer userId, Integer specId, Integer num, Integer cardId, Integer forwardId) {
        // 判断商品规格库存
        GoodSpec goodSpecModel = goodSpecMapperExt.selectByPrimaryKey(specId);
        AssertUtil.isFalse(goodSpecModel == null, "商品规格失效，请重新筛选！");
        AssertUtil.isFalse(goodSpecModel.getSpecNum() < num, "库存不足！");

        Goods goods = goodsBaseService.getOn(goodSpecModel.getGoodsId());
        AssertUtil.nonNull(goods, ResultEnum.PARAM_CHECK, "商品已下架");

        ShopCart shopcartModel = shopCartMapperExt.selectShopcartSpec(userId, specId);
        if (shopcartModel != null && shopcartModel.getShopCartId() > 0) {
            // 以前有购物车记录，就修改
            ShopCart model = new ShopCart();
            model.setShopCartId(shopcartModel.getShopCartId());
            model.setNum(num + shopcartModel.getNum());
            model.setForwardId(forwardId);
            shopCartMapperExt.updateByPrimaryKeySelective(model);
        } else {
            // 添加购物车
            shopcartModel = new ShopCart();
            shopcartModel.setUserId(userId);
            shopcartModel.setCompanyId(goods.getCompanyId());
            shopcartModel.setGoodsId(goods.getGoodsId());
            shopcartModel.setSpecId(specId);
            shopcartModel.setNum(num);
            shopcartModel.setCardId(cardId);
            shopcartModel.setForwardId(forwardId);
            shopcartModel.setCreateTime(DateUtil.currentDate());
            shopCartMapperExt.insertSelective(shopcartModel);
        }
    }

    @Override
    public void remove(Integer shopCartId) {
        shopCartMapperExt.deleteByPrimaryKey(shopCartId);
    }

    @Override
    public void edit(Integer shopCartId, Integer num) {
        ShopCart shopCart = new ShopCart();
        shopCart.setShopCartId(shopCartId);
        shopCart.setNum(num);
        shopCartMapperExt.updateByPrimaryKeySelective(shopCart);
    }

    @Override
    public List<ShopCart> list(Integer userId) {
        return shopCartMapperExt.selectList(userId);
    }

    @Override
    public Integer count(Integer userId) {
        return shopCartMapperExt.selectCount(userId);
    }
}
